﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl x:Class="Microsoft.Windows.Controls.Samples.CustomEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
    <StackPanel>

        <ContentControl Content="Populating and Populated events" Style="{StaticResource Header}" />
        <StackPanel>
            <TextBlock Style="{StaticResource Information}">
                The AutoCompleteBox control is able to work with unbound data as 
                well. By subscribing to the Populating event of an 
                AutoCompleteBox control, as a developer you can cancel the 
                population, change ItemsSource data, or otherwise react to the 
                input before the control provides suggestions.
            </TextBlock>
        </StackPanel>

        <ContentControl Content="Text completion off" Style="{StaticResource Header}" />
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="230" />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Padding="5">Immediate population:</TextBlock>

            <controls:AutoCompleteBox 
                x:Name="NowAutoComplete" 
                SearchMode="None"
                Width="300" 
                IsTextCompletionEnabled="False"
                Grid.Column="1" 
                HorizontalAlignment="Left"
                Margin="0, 0, 0, 12"
                Tag="Value1"
                SelectionChanged="OnSelectionChanged"
                />
            <ContentPresenter x:Name="Value1" Grid.Column="2" />

            <TextBlock Padding="5" Grid.Row="1">Delayed population:</TextBlock>

            <controls:AutoCompleteBox 
                x:Name="LaterAutoComplete" 
                SearchMode="None"
                IsTextCompletionEnabled="False"
                Width="300" 
                Grid.Row="1" 
                Grid.Column="1" 
                HorizontalAlignment="Left" 
                Tag="Value2"
                SelectionChanged="OnSelectionChanged"
            />
            <ContentPresenter x:Name="Value2" Grid.Row="1" Grid.Column="2" />

        </Grid>

        <ContentControl Content="Text completion on" Style="{StaticResource Header}" />
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="230" />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Padding="5">Immediate population:</TextBlock>

            <controls:AutoCompleteBox 
                x:Name="NowAutoComplete2" 
                SearchMode="None"
                IsTextCompletionEnabled="True"
                Width="300" 
                Grid.Column="1" 
                HorizontalAlignment="Left"
                Margin="0, 0, 0, 12"
                Tag="Value3"
                SelectionChanged="OnSelectionChanged"
                />
            <ContentPresenter x:Name="Value3" Grid.Column="2"/>

            <TextBlock Padding="5" Grid.Row="1">Delayed population:</TextBlock>

            <controls:AutoCompleteBox
                x:Name="LaterAutoComplete2"
                SearchMode="None"
                IsTextCompletionEnabled="True"
                Width="300"
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Left"
                Tag="Value4"
                SelectionChanged="OnSelectionChanged"
                />
            <ContentPresenter x:Name="Value4"  Grid.Row="1" Grid.Column="2"/>
            
        </Grid>

        <src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="CustomEvents.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl x:Class="Microsoft.Windows.Controls.Samples.CustomEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"&gt;
    &lt;StackPanel&gt;

        &lt;ContentControl Content="Populating and Populated events" Style="{StaticResource Header}" /&gt;
        &lt;StackPanel&gt;
            &lt;TextBlock Style="{StaticResource Information}"&gt;
                The AutoCompleteBox control is able to work with unbound data as 
                well. By subscribing to the Populating event of an 
                AutoCompleteBox control, as a developer you can cancel the 
                population, change ItemsSource data, or otherwise react to the 
                input before the control provides suggestions.
            &lt;/TextBlock&gt;
        &lt;/StackPanel&gt;

        &lt;ContentControl Content="Text completion off" Style="{StaticResource Header}" /&gt;
        &lt;Grid&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="230" /&gt;
                &lt;ColumnDefinition /&gt;
                &lt;ColumnDefinition /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition /&gt;
                &lt;RowDefinition /&gt;
            &lt;/Grid.RowDefinitions&gt;
            &lt;TextBlock Padding="5"&gt;Immediate population:&lt;/TextBlock&gt;

            &lt;controls:AutoCompleteBox 
                x:Name="NowAutoComplete" 
                SearchMode="None"
                Width="300" 
                IsTextCompletionEnabled="False"
                Grid.Column="1" 
                HorizontalAlignment="Left"
                Margin="0, 0, 0, 12"
                Tag="Value1"
                SelectionChanged="OnSelectionChanged"
                /&gt;
            &lt;ContentPresenter x:Name="Value1" Grid.Column="2" /&gt;

            &lt;TextBlock Padding="5" Grid.Row="1"&gt;Delayed population:&lt;/TextBlock&gt;

            &lt;controls:AutoCompleteBox 
                x:Name="LaterAutoComplete" 
                SearchMode="None"
                IsTextCompletionEnabled="False"
                Width="300" 
                Grid.Row="1" 
                Grid.Column="1" 
                HorizontalAlignment="Left" 
                Tag="Value2"
                SelectionChanged="OnSelectionChanged"
            /&gt;
            &lt;ContentPresenter x:Name="Value2" Grid.Row="1" Grid.Column="2" /&gt;

        &lt;/Grid&gt;

        &lt;ContentControl Content="Text completion on" Style="{StaticResource Header}" /&gt;
        &lt;Grid&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="230" /&gt;
                &lt;ColumnDefinition /&gt;
                &lt;ColumnDefinition /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition /&gt;
                &lt;RowDefinition /&gt;
            &lt;/Grid.RowDefinitions&gt;
            &lt;TextBlock Padding="5"&gt;Immediate population:&lt;/TextBlock&gt;

            &lt;controls:AutoCompleteBox 
                x:Name="NowAutoComplete2" 
                SearchMode="None"
                IsTextCompletionEnabled="True"
                Width="300" 
                Grid.Column="1" 
                HorizontalAlignment="Left"
                Margin="0, 0, 0, 12"
                Tag="Value3"
                SelectionChanged="OnSelectionChanged"
                /&gt;
            &lt;ContentPresenter x:Name="Value3" Grid.Column="2"/&gt;

            &lt;TextBlock Padding="5" Grid.Row="1"&gt;Delayed population:&lt;/TextBlock&gt;

            &lt;controls:AutoCompleteBox
                x:Name="LaterAutoComplete2"
                SearchMode="None"
                IsTextCompletionEnabled="True"
                Width="300"
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Left"
                Tag="Value4"
                SelectionChanged="OnSelectionChanged"
                /&gt;
            &lt;ContentPresenter x:Name="Value4"  Grid.Row="1" Grid.Column="2"/&gt;
            
        &lt;/Grid&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="CustomEvents.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

// Global suppressions for this sample
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "Microsoft.Windows.Controls.Samples.CustomEvents.#Value1")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "Microsoft.Windows.Controls.Samples.CustomEvents.#Value2")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "Microsoft.Windows.Controls.Samples.CustomEvents.#Value3")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "Microsoft.Windows.Controls.Samples.CustomEvents.#Value4")]

namespace Microsoft.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// The PopulationEvents class shows how a developer might hook into the 
    /// population events to provide custom data.
    /// &lt;/summary&gt;
    [Sample("(2)Custom Events", DifficultyLevel.Basic)]
    [Category("AutoCompleteBox")]
    public partial class CustomEvents : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the type.
        /// &lt;/summary&gt;
        public CustomEvents()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        /// &lt;summary&gt;
        /// Handle the Loaded event of the page.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event arguments.&lt;/param&gt;
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            NowAutoComplete.Populating += OnPopulatingSynchronous;
            NowAutoComplete2.Populating += OnPopulatingSynchronous;
            LaterAutoComplete.Populating += OnPopulatingAsynchronous;
            LaterAutoComplete2.Populating += OnPopulatingAsynchronous;
        }

        /// &lt;summary&gt;
        /// The Populating event handler.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnPopulatingSynchronous(object sender, PopulatingEventArgs e)
        {
            AutoCompleteBox source = (AutoCompleteBox)sender;

            source.ItemsSource = new string[]
            {
                e.Parameter + "1",
                e.Parameter + "2",
                e.Parameter + "3",
            };
        }

        /// &lt;summary&gt;
        /// The populating handler.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnPopulatingAsynchronous(object sender, PopulatingEventArgs e)
        {
            AutoCompleteBox source = (AutoCompleteBox)sender;

            // Cancel the populating value: this will allow us to call 
            // PopulateComplete as necessary.
            e.Cancel = true;
            
            // Use the dispatcher to simulate an asynchronous callback when 
            // data becomes available
            Dispatcher.BeginInvoke(
                delegate
                {
                    source.ItemsSource = new string[]
                    {
                        e.Parameter + "1",
                        e.Parameter + "2",
                        e.Parameter + "3",
                    };

                    // Population is complete
                    source.PopulateComplete();
                });
        }

        /// &lt;summary&gt;
        /// Called when an AutoCompleteBox's selected value changes. Uses the 
        /// Tag property to identify the content presenter to be updated.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source AutoCompleteBox control.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler is wired up in XAML.")]
        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            AutoCompleteBox acb = (AutoCompleteBox)sender;

            // In these sample scenarios, the Tag is the name of the content 
            // presenter to use to display the value.
            string contentPresenterName = (string)acb.Tag;
            ContentPresenter cp = FindName(contentPresenterName) as ContentPresenter;
            if (cp != null)
            {
                cp.Content = acb.SelectedItem;
            }
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>
